"""
Smart Review Router
"""

from fastapi import APIRouter
import logging

from ..models.review_models import ReviewRequest, ReviewResponse
from ..services.review_service import review_service

logger = logging.getLogger(__name__)

router = APIRouter(
    prefix="/api/v1/review",
    tags=["Smart Review"]
)


@router.post("/", response_model=ReviewResponse, summary="Smart Review")
async def review_document(request: ReviewRequest):
    """
    Smart Review API

    Args:
        request: Review request with question, grading_criteria, student_answer

    Returns:
        Review result with feedback
    """
    try:
        logger.info(f"Review request: user={request.user_id}, question={request.question[:50]}...")

        success, result, error = review_service.simple_review(
            user_id=request.user_id,
            question=request.question,
            grading_criteria=request.grading_criteria,
            student_answer=request.student_answer,
            student_answer_file=request.student_answer_file,
            document_name=request.document_name or "student_answer.txt"
        )

        if success and result:
            return ReviewResponse(
                success=True,
                message="Review completed",
                result=result
            )
        else:
            return ReviewResponse(
                success=False,
                message="Review failed",
                result=None,
                error=error or "Unknown error"
            )

    except Exception as e:
        logger.error(f"Review error: {e}")
        return ReviewResponse(
            success=False,
            message="Internal error",
            result=None,
            error=str(e)
        )
